package com.shoeHouse.user.service;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.shoeHouse.user.dao.UserInfoMapper;
import com.shoeHouse.user.model.UserInfo;
import com.shoeHouse.user.model.UserInfoExample;
import com.shoeHouse.user.model.UserInfoExample.Criteria;
import com.shoeHouse.util.PageHelper;

@Service
public class UserInfoServiceImpl implements UserInfoService{
      @Autowired
      private UserInfoMapper userInfoMapper;
    
	@Override
	public void save(UserInfo o) {
		// TODO Auto-generated method stub
		userInfoMapper.insert(o);
	}
    
	@Override
	public void deleteAll(String ids) {
		// TODO Auto-generated method stub
		UserInfoExample example=new UserInfoExample();
		Criteria criteria=example.createCriteria();
		userInfoMapper.deleteByExample(example);
	}

	@Override
	public void removeAll(String ids) {
		// TODO Auto-generated method stub
		System.out.println(ids);
		String[] id=ids.split(",");
		for(int i=0;i<id.length;i++){
			System.out.println(id[i]);
		UserInfo record=new UserInfo();
		
		record.setStatus(Byte.parseByte("1"));
		UserInfoExample example=new UserInfoExample();
		Criteria criteria=example.createCriteria();
        if(id!=null&&!id.equals("")){
        	criteria.andIdEqualTo(id[i]);
		}
		userInfoMapper.updateByExampleSelective(record, example);
		}
	}

	@Override
	public void update(UserInfo o) {
		// TODO Auto-generated method stub
		UserInfoExample example=new UserInfoExample();
		Criteria criteria=example.createCriteria();
	    criteria.andIdEqualTo(o.getId());

		/*if(null!=o.getUsername()&&!o.getUsername().equals("")){
			
			criteria.andUsernameEqualTo(o.getUsername());
		}
        if(null!=o.getPassword()&&!o.getPassword().equals("")){
			
			criteria.andUsernameEqualTo(o.getPassword());
		}
		if(null!=o.getAddress()&&o.getAddress().equals("")){
			criteria.andAddressEqualTo(o.getAddress());
		}
        if(null!=o.getPhone()&&o.getPhone().equals("")){
			criteria.andPhoneEqualTo(o.getPhone());
		}
        if(null!=o.getRoleId()&&o.getRoleId().equals("")){
			criteria.andRoleIdEqualTo(o.getRoleId());
      		}
        if(null!=o.getDepartmentId()&&o.getDepartmentId().equals("")){
			criteria.andDepartmentIdEqualTo(o.getDepartmentId());
  		}
       if(null!=o.getGmtModified()&&o.getGmtModified().equals("")){
			criteria.andGmtModifiedEqualTo(o.getGmtModified());
  		}
       if(null!=o.getName()&&o.getName().equals("")){
			criteria.andNameEqualTo(o.getName());
 		}
    
       if(null!=o.getSex()&&o.getSex().equals("")){
    	   
    	   criteria.andSexEqualTo(o.getSex());
       }
      if(null!=o.getPicture()&&o.getPicture().equals("")){
    	   
    	   criteria.andPhoneEqualTo(o.getPhone());
       }*/
      userInfoMapper.updateByExampleSelective(o, example);
	}

	@Override
	public int count() {
		// TODO Auto-generated method stub
		UserInfoExample example=new UserInfoExample();
		Criteria criteria=example.createCriteria();
		criteria.andStatusEqualTo(Byte.parseByte("0"));
		int count=userInfoMapper.countByExample(example);
		return count;
	}
    
	@Override
	public List<UserInfo> getAll(HttpServletRequest request,UserInfo userInfo,PageHelper pageHelper) {
		
		// TODO Auto-generated method stub
		String limit="limit "+(pageHelper.getCurrentPage()-1)*pageHelper.getPageSize()+","+pageHelper.getPageSize();
		UserInfoExample example=new UserInfoExample();
		example.setOrderByClause("gmt_modified DESC "+limit);
	
		Criteria criteria=example.createCriteria();
		
		criteria.andStatusEqualTo(Byte.parseByte("0"));
		if(null!=userInfo.getId()&&!userInfo.getId().equals("")){
			criteria.andIdEqualTo(userInfo.getId());
			
		}
		System.out.println("#######"+request.getParameter("beginTime"));
		if(request.getParameter("beginTime")!=null&&!request.getParameter("beginTime").equals("")){
			System.out.println("cccppppp");
			SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");  
			try {
if(request.getParameter("endTime")==null){
	criteria.andGmtModifiedBetween(sdf.parse(request.getParameter("beginTime")),new Date());
			

}else{
		
				System.out.println(request.getParameter("beginTime"));
				System.out.println("yyyyyyccc");
				criteria.andGmtModifiedBetween(sdf.parse(request.getParameter("beginTime")),sdf.parse(request.getParameter("endTime")));
		}
			} catch (ParseException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
				System.out.println("ccccccc");
			}
			
		}
		if(null!=userInfo.getSex()&&!userInfo.getSex().equals("")){
			criteria.andSexEqualTo(userInfo.getSex());
			
		}
		if(null!=userInfo.getUsername()&&!userInfo.getUsername().equals("")){
			
			criteria.andUsernameEqualTo(userInfo.getUsername());
		}
        if(null!=userInfo.getPassword()&&!userInfo.getPassword().equals("")){
			
			criteria.andUsernameEqualTo(userInfo.getPassword());
		}
         if(null!=userInfo.getName()&&!userInfo.getName().equals("")){
			
			criteria.andNameEqualTo(userInfo.getName());
		}
         if(null!=userInfo.getDepartmentId()&&!userInfo.getDepartmentId().equals("")){
 			
 			criteria.andDepartmentIdEqualTo(userInfo.getDepartmentId());
 		}
		List<UserInfo> list=userInfoMapper.selectByExample(example);
		return list;
	}

	@Override
	public UserInfo getSingle(UserInfo userInfo) {
		// TODO Auto-generated method stub
		UserInfoExample example=new UserInfoExample();
		Criteria criteria=example.createCriteria();
		criteria.andStatusEqualTo(Byte.parseByte("0"));
		if(null!=userInfo.getId()&&!userInfo.getId().equals("")){
			criteria.andIdEqualTo(userInfo.getId());
			
		}
		
		if(null!=userInfo.getUsername()&&!userInfo.getUsername().equals("")){
			
			criteria.andUsernameEqualTo(userInfo.getUsername());
		}
        if(null!=userInfo.getPassword()&&!userInfo.getPassword().equals("")){
			
			criteria.andUsernameEqualTo(userInfo.getPassword());
		}
		UserInfo userInfos;
		try {
			userInfos = userInfoMapper.selectByExample(example).get(0);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			return null;
		}
		return userInfos;
	}
	
     
	
}
